package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.RegGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.Subinfo;
import com.ruoyi.system.mapper.SubMapper;
import com.ruoyi.system.domain.Sub;
import com.ruoyi.system.service.ISubService;

/**
 * 属性值回显Service业务层处理
 * 
 * @author yaozh
 * @date 2024-03-14
 */
@Service
public class SubServiceImpl implements ISubService 
{
    private static final Logger log = LoggerFactory.getLogger(SubServiceImpl.class);
    @Autowired
    private SubMapper subMapper;

    /**
     * 查询属性值回显
     * 
     * @param subid 属性值回显主键
     * @return 属性值回显
     */
    @Override
    public Sub selectSubBySubid(Long subid, String dbName)
    {
        return subMapper.selectSubBySubid(subid, dbName);
    }

    /**
     * 查询属性值回显列表
     * 
     * @param sub 属性值回显
     * @return 属性值回显
     */
    @Override
    public List<Sub> selectSubList(Sub sub, String dbName)
    {
        return subMapper.selectSubList(sub, dbName);
    }

    @Override
    public int insertObjectForGetSubId(String dBname, Sub sub) {
        int rows=0;
        try {
            rows=subMapper.insertObjectForGetSubId(dBname,sub);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("保存失败");
        }
        return rows;
    }

    /**
     * 新增属性值回显
     * 
     * @param sub 属性值回显
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSub(Sub sub, String dbName)
    {
        int rows = subMapper.insertSub(sub, dbName);
        insertSubinfo(sub, dbName);
        return rows;
    }

    /**
     * 修改属性值回显
     * 
     * @param sub 属性值回显
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSub(Sub sub, String dbName)
    {
        subMapper.deleteSubinfoBySubid(sub.getSubid(), dbName);
        insertSubinfo(sub, dbName);
        return subMapper.updateSub(sub, dbName);
    }

    /**
     * 更新阈值信息
     * @param dBname
     * @param sub
     * @return
     */
    @Override
    public int updateObject(String dBname, Sub sub) {
        int rows=0;
        try {
            rows=subMapper.updateObject(dBname,sub);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("更新失败");
        }
        return rows;
    }

    /**
     * 批量删除属性值回显
     * 
     * @param subids 需要删除的属性值回显主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSubBySubids(Long[] subids, String dbName)
    {
        subMapper.deleteSubinfoBySubids(subids, dbName);
        return subMapper.deleteSubBySubids(subids, dbName);
    }

    /**
     * 删除属性值回显信息
     * 
     * @param subid 属性值回显主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSubBySubid(Long subid, String dbName)
    {
        subMapper.deleteSubinfoBySubid(subid, dbName);
        return subMapper.deleteSubBySubid(subid, dbName);
    }

    /**
     * 新增数据回显值信息
     * 
     * @param sub 属性值回显对象
     */
    public void insertSubinfo(Sub sub, String dbName)
    {
        List<Subinfo> subinfoList = sub.getSubinfoList();
        Long subid = sub.getSubid();
        if (StringUtils.isNotNull(subinfoList))
        {
            List<Subinfo> list = new ArrayList<Subinfo>();
            for (Subinfo subinfo : subinfoList)
            {
                subinfo.setSubid(subid);
                list.add(subinfo);
            }
            if (list.size() > 0)
            {
                subMapper.batchSubinfo(list, dbName);
            }
        }
    }

    @Override
    public String importSub(List<Sub> subList, Boolean isUpdateSupport, String operaName, String dbName){
        if (subList == null || subList.size() == 0)
        {
            throw new ServiceException("导入项目属性回显数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Sub sub : subList)
        {
            try{
                // 验证是否存在
                String subName = sub.getSubname();
                Sub tempSub = subMapper.selectSubBySubName(subName,dbName);
                if (tempSub == null)
                {
                    sub.setCreateBy(operaName);
                    subMapper.insertSub(sub,dbName);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、属性回显定义名称 " + sub.getSubname() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    sub.setSubid(tempSub.getSubid());
                    sub.setUpdateBy(operaName);
                    subMapper.updateSub(sub,dbName);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、属性回显定义名称 " + sub.getSubname() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、属性回显定义名称 " + sub.getSubname() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、属性回显定义名称 " + sub.getSubname() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }

        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
